// pages/detail/etail.js
const app=getApp();
const get = require('../../utils/common.js')
var WxParse = require('../../wxParse/wxParse.js');
Page({
  data: {
    goods_name:'',
    goods_id:'',
    sale_price:0,
    goods_img:[]
  },
  onLoad: function (options) {
    var that = this
    var urlId = wx.getStorageSync('urlId')
   
    if (urlId){
      that.setData({urlId:urlId,moRenId:urlId}) //从url进来
   
    }else {
      let moRenId =  options.id || options.sid || options.moRenId//列表页进来 和 顶部分享进来; moRenId是已授权进入的时候传的参数 sid是未授权顶部分享进入传过来的参数
      that.setData({moRenId:moRenId})
     
    }
    try {
      var urlOpenid = wx.getStorageSync('urlOpenid')
    
      if (urlOpenid) {
       
        that.setData({urlOpenid:urlOpenid,openid:urlOpenid})
      }else{
        let openid=wx.getStorageSync('openid')
        that.setData({openid:openid})
      } 
    } catch (e) {
     
    }

    wx.getStorage({
      key: 'telNum',
      success: function (res) {
        that.setData({telNum:res.data})
      }
    })
    that.getUserInfoStorage()
    setTimeout(()=>{
      that.fromMoRen()
    },500)
    
  },
    //默认的sid请求数据
    fromMoRen(res){ 
      let that = this
   
      wx.request({
        url: 'https://sim.xsmaofa.com/index/goods/goodsdetail',
        method:'post',
        data:{
          id:that.data.moRenId
        },
        success(res){
         // console.log("默认sid请求的detail",res.data.data,that.data.moRenId)
          var article = res.data.data.detail
          WxParse.wxParse('article', 'html', article, that,5)
        
          that.setData({
            detail:res.data.data.detail,
            goods_id:res.data.data.id,
            goods_img:res.data.data.goods_img,
            title_img:res.data.data.title_img,
            goods_name:res.data.data.goods_name,
            goods_desc:res.data.data.goods_desc,
            price:res.data.data.price,
            sale_price:res.data.data.sale_price,
            stock:res.data.data.stock,
            link_type:res.data.data.link_type,
          })
        }
      })
    },

  //取缓存openid & session_key
  getUserInfoStorage(){
    let that=this
    let openid=wx.getStorageSync('openid')
    let session_key=wx.getStorageSync('session_key')
    that.setData({
      openid:openid,
      session_key:session_key
    })
  
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    let that = this
    var sid = that.data.goods_id
    var openid = that.data.openid
    var title_img = that.data.title_img
    var goods_name = that.data.goods_name
    var link_type = that.data.link_type
    //console.log(sid,openid,title_img,link_type,999)
    　　var shareObj = {
    　　　　title: goods_name,        
    　　　　path:  "/pages/login/login?sid=" + sid + '&openid=' + openid + '&link_type=' + link_type,
    　　　　imageUrl: title_img,    
    　　}
    　　return shareObj
  },
    //回到顶部
    toTop: function(e) {
    //  console.log(e) // 一键回到顶部
      if (wx.pageScrollTo) {
        wx.pageScrollTo({
          scrollTop: 0
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
        })
      }
    },
  //回到收藏
  toShoucang(e) {
  
  wx.redirectTo({
    url: '/pages/list/list',
  })
}
})